package com.itheima.test.test16;

import java.util.Collections;
import java.util.Comparator;
import java.util.TreeSet;

public class StudentDemo {
    public static void main(String[] args) {
        //创建一个set集合按照年龄降序,年龄相同时,根据学号升序排列
        TreeSet<Student> set = new TreeSet<Student>((o1,o2)->{
            int result=o2.getAge()-o1.getAge();
            result=result==0?o1.getId().compareTo(o2.getId()):result;
            return result;});
        //往集合中添加三个学生对象
        Collections.addAll(set,new Student("张三",18,"heima001"),
                new Student("李四",18,"heima002"),
                new Student("王五",17,"heima003"));
        //将排序好的集合进行遍历,输出每个对象的属性值.
        set.forEach(stu-> System.out.println(stu.toString()));

    }
}
